package com.su.kddq.config;

import com.su.kddq.constant.RabbitMqConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sx
 * RabbitMQ初始化交换机、队列
 */
@Configuration
public class RabbitMqConfig {

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange dlxExchange(){
        return new DirectExchange(RabbitMqConstant.EXCHANGE_DLX,true,false);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue dlxQueue(){
        return new Queue(RabbitMqConstant.QUEUE_DLX);
    }

    @Bean
    public Binding dlxBing(){
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(RabbitMqConstant.ROUTING_KEY_DLX);
    }


    /**
     * 用户充值交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(RabbitMqConstant.EXCHANGE_ORDER, true, false);
    }

    /**
     * 取件员提现交换机
     * @return
     */
    @Bean
    public DirectExchange txExchange(){
        return new DirectExchange(RabbitMqConstant.EXCHANGE_TX,true,false);
    }

    /**
     * 用户充值队列
     * @return
     */
    @Bean
    public Queue queue() {
        Map<String,Object> arguments = new HashMap<>();
        // 绑定该队列到私信交换机
        arguments.put("x-dead-letter-exchange",RabbitMqConstant.EXCHANGE_DLX);
        arguments.put("x-dead-letter-routing-key",RabbitMqConstant.ROUTING_KEY_DLX);
        return new Queue(RabbitMqConstant.QUEUE_ORDER,true, false, false,arguments);
    }

    /**
     * 取件员提现队列
     * @return
     */
    @Bean
    public Queue txQueue(){
        return new Queue(RabbitMqConstant.QUEUE_TX,true,false,false);
    }

    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(directExchange()).with(RabbitMqConstant.ROUTING_KEY);
    }


    @Bean
    public Binding txBind(){
        return BindingBuilder.bind(txQueue()).to(txExchange()).with(RabbitMqConstant.ROUTING_KEY_TX);
    }

    /**
     * 消息推送交换机
     * @return
     */
    @Bean
    public DirectExchange msgExchange(){
        return new DirectExchange(RabbitMqConstant.EXCHANGE_MSG_CID, true, false);
    }

    /**
     * 消息推送队列
     * @return
     */
    @Bean
    public Queue msgQueue(){
        return new Queue(RabbitMqConstant.QUEUE_MSG);
    }

    @Bean
    public Binding msgBinging(){
        return  BindingBuilder.bind(msgQueue()).to(msgExchange()).with(RabbitMqConstant.ROUTING_KEY_MSG);
    }

}
